
import axios from 'axios'
// import qs from 'qs'
const instance = axios.create({
  baseURL: '',
  timeout: 50000,
  headers: { 'Content-Type': 'application/json;charset=UTF-8' }
})


//  拦截器 - 请求拦截
instance.interceptors.request.use(
  config => {
    // 部分接口需要拿到 token
    let token = localStorage.getItem('token');
    // console.log(token);
    if (token) {
      config.headers['aa-token'] = token;
    }
    return config;
  }, 
  err => {
    // 对请求错误做些什么
    return Promise.reject(err);
  }
)


// 响应拦截
instance.interceptors.response.use(res => {
  // console.log('响应拦截')
  if (res.headers.token) {
    var token = res.headers.token
    // console.log(token)
    localStorage.setItem('token', token)
  }
  return res
}, err => {
  return Promise.reject(err)
})

export default instance
